package com.ys.nms.module.business.service.ports;

import cn.hutool.core.collection.CollUtil;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.ports.vo.*;
import com.ys.nms.module.business.dal.dataobject.ports.PortsDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.ports.PortsMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 端口 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class PortsServiceImpl implements PortsService {

    @Resource
    private PortsMapper portsMapper;

    @Override
    public Integer createPorts(PortsSaveReqVO createReqVO) {
        // 插入
        PortsDO ports = BeanUtils.toBean(createReqVO, PortsDO.class);
        portsMapper.insert(ports);

        // 返回
        return ports.getPortId();
    }

    @Override
    public void updatePorts(PortsSaveReqVO updateReqVO) {
        // 校验存在
        validatePortsExists(updateReqVO.getPortId());
        // 更新
        PortsDO updateObj = BeanUtils.toBean(updateReqVO, PortsDO.class);
        portsMapper.updateById(updateObj);
    }

    @Override
    public void deletePorts(Integer id) {
        // 校验存在
        validatePortsExists(id);
        // 删除
        portsMapper.deleteById(id);
    }

    @Override
        public void deletePortsListByIds(List<Integer> ids) {
        // 删除
        portsMapper.deleteByIds(ids);
        }


    private void validatePortsExists(Integer id) {
        if (portsMapper.selectById(id) == null) {
            throw exception(PORTS_NOT_EXISTS);
        }
    }

    @Override
    public PortsDO getPorts(Integer id) {
        return portsMapper.selectById(id);
    }

    @Override
    public PageResult<PortsDO> getPortsPage(PortsPageReqVO pageReqVO) {
        return portsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PortsDO> getPortsListByDevicesId(PortsPageReqVO pageReqVO) {
       LambdaQueryWrapperX<PortsDO> queryWrapperX =  new LambdaQueryWrapperX<PortsDO>();
        queryWrapperX.eqIfPresent(PortsDO::getPortId, pageReqVO.getPortId());
        queryWrapperX.eq(PortsDO::getDeviceId, pageReqVO.getDeviceId());
        return  portsMapper.selectList(queryWrapperX);
    }

}